package com.example.studentmanager;

import androidx.appcompat.app.AppCompatActivity;

import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class AddStudent extends AppCompatActivity {
    EditText xuehao, gender, adress, phone, chenji;
    Button btnadd;
    StudentDB studentDB;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_student);
        studentDB = StudentDB.getStudentDB(AddStudent.this);
        xuehao = findViewById(R.id.xuehao);
        gender = findViewById(R.id.gender);
        adress = findViewById(R.id.adress);
        phone = findViewById(R.id.phone);
        chenji = findViewById(R.id.chengji);
        btnadd = findViewById(R.id.btnadd);


        btnadd.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                if (xuehao.getText().toString().isEmpty() ||
                        gender.getText().toString().isEmpty() ||
                        adress.getText().toString().isEmpty() ||
                        phone.getText().toString().isEmpty() ||
                        chenji.getText().toString().isEmpty()) {
                    Toast.makeText(AddStudent.this, "输入的信息有空！", Toast.LENGTH_SHORT).show();

                }
                Student student = new Student();
                student.setXuehao(xuehao.getText().toString());
                student.setGender(gender.getText().toString());
                student.setAddress(adress.getText().toString());
                student.setPhone(phone.getText().toString());
                student.setChengji(chenji.getText().toString());
                boolean result;
                result = studentDB.insert(student);
                if (result) {

                    Toast.makeText(AddStudent.this, "插入成功", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(AddStudent.this, "添加失败，学号已重复，请重新输入", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}